<?php
declare (strict_types = 1);

namespace app\adminapi\controller\file;

use app\adminapi\validate\AdminValidate;
use app\common\controller\BaseController;
use app\common\service\file\FileUploadsService;
use think\Request;

class FileUploads extends BaseController
{
    /**
     * 构造函数依赖注入
     * 
     * @param FileUploadsService $service
     */
    public function __construct(FileUploadsService $service)
    {
        $this->service = $service;
    }

    /**
     * 获取文件列表
     *
     * @param Request $request
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     */
    public function index(Request $request): \think\Response
    {
        $params = $request->param();
        $where = [];
        if (isset($params['stored_name']) && !empty($params['stored_name'])) {
            $where[] = ['stored_name', 'like', '%' . $params['stored_name'] . '%'];
        }
        if (isset($params['cid']) && !empty($params['cid'])) {
            $where[] = ['cid', '=', $params['cid']];
        }
        if (isset($params['type']) && !empty($params['type'])) {
            $where[] = ['file_type', '=', $params['type']];
        }
        return $this->success($this->service->getList($where));
    }

    /**
     * 重命名文件
     *
     * @param $id
     * @param Request $request
     * @param AdminValidate $validate
     * @return \think\Response
     */
    public function update($id, Request $request)
    {
        $data = $request->param();
        return $this->success($this->service->update($data, $id));
    }

    /**
     * 移动到其他分类
     *
     * @param Request $request
     * @return \think\Response
     */
    public function move(Request $request): \think\Response
    {
        $data = $request->param();
        return $this->success($this->service->move($data));
    }

    /**
     * 删除文件
     *
     * @param Request $request
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(Request $request): \think\Response
    {
        $where = $request->param();
        return $this->success($this->service->delete($where));
    }

    /**
     * 上传文件
     *
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function upload(Request $request): \think\Response
    {
        return $this->success($this->service->upload());
    }
}
